Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java static keyword

    The static keyword in Java primarily functions as a tool for memory management. In Java, the static keyword enables the sharing of variables or methods across multiple instances of a class. Users can apply the static keyword to variables, methods, blocks, and nested classes. Static keyword belongs to the class itself rather than an object of the class. When a variable or method needs to apply to every instance of a class as a constant, the static keyword is employed.



Table Of Content

  • Java static keyword
  • Java static Variable
  • Java static Method
  • Java static Block
  • Java static Classes


A static member (block, variable, function, or nested class) must be created by adding the term static before its definition. When a class member is designated as static, it can be accessed without an object reference and before the class's objects are created.
The main method of a class is generally static.


Note: If you want to construct a static member (a block, variable, method, or nested class), use the keyword static before its definition.




The static keyword is a non-access modifier and can be used for the following in the Java programming language.


Static keyword can be used as:

  • Java static Variable
  • Java static Method
  • Java static Block
  • Java static Classes

Java static variable

A variable is considered static if you declare it to be static keyword before variable.

  • When a variable is defined as static, it creates one copy and shared by all objects within the class. Essentially, static variables are global variables. The same static variable is shared to all class objects.
  • Only at the class level may static variables be created. When the class is loaded, static variable only receives one memory allocation within the class area.
  • Static variables and blocks are executed in the order that they appear in a programme.




Problem without Static Keyword

 class   Company{ 
  int   Empid; 
  String EmpName;  
  String  Company="DockerTpoint";  
  } 


Assuming my company has 2000 employees, each time an object is created, all instance data members will receive RAM. Since each employee has a distinct EmpName and Empid, using an instance data member is appropriate in this situation. The term "Company" here refers to the attribute that all objects share. If we set it to a static, the memory will only be retrieved once for this particular field.


How to declare static variable
 // Java  program using static variable  
class Emp{  
    int EmpId;
    String EmpName;  
    static String CompanyName ="DockerTpoint";
        Emp(int EmpId, String EmpName ){  
            this.EmpId = EmpId;  
            this.EmpName = EmpName;  
        }   
        void display(){
            System.out.println("Welcome to DockerTpoint");
            System.out.println("Your Empid is "+EmpId+",Your official name is "+EmpName+
                " & Your Company name is "+CompanyName+".\n");
         }  
}  
public class Company{  
    public static void main(String args[]){  
        Emp emp1 = new Emp(40139522,"Meena");  
        Emp emp2 = new Emp(40139524,"Alok");  
        //we can change the Company of all objects by uncomment below line of code  
        //Emp.Company="ProPoint";  
        emp1.display();  
        emp2.display();  
    }  
}
    



Output:

Welcome to DockerTpoint 
Your Empid is 40139522,Your Officia name is Meena & Your Company name is DockerTpoint.

Welcome to DockerTpoint 
Your Empid is 40139524,Your Officia name is Alok & Your Company name is DockerTpoint.


Java static Method

A method is said to as static when the static keyword is used in its declaration. The main() function is the most typical illustration of a static method.It is possible to access any static members without objects of its class.
There are various limitations on methods that are marked static:

  • A static method, is directly linked to the class itself, unlike other methods associated with objects
  • It is possible to call a static method without first creating a class object.
  • A static method have the ability to access the values of static data members.
  • A static method cannot be make in any way reference to this or super.

Restrictions on static method
  • Static methods cannot directly access or call non-static methods or data members.
  • Super and this cannot be used together in a static keyword



How to declare static method
 // Java  program using static variable  
class Emp{  
    int EmpId;
    String EmpName;  
    static String CompanyName ="DockerTpoint";
    Emp(int EmpId, String EmpName ){  
        this.EmpId = EmpId;  
        this.EmpName = EmpName;  
    }
    static void CompanyChange(){  
        CompanyName = "ProPoint";  
     }  
    void display(){
     System.out.println("Welcome to "+CompanyName);
     System.out.println("Your Empid is "+EmpId+",Your official name is "+EmpName+
         " & Your Company name is "+CompanyName+".\n");
     }  
}  
public class Company{  
   public static void main(String args[]){
       Emp.CompanyChange();
       Emp emp1 = new Emp(40139522,"Meena");  
       Emp emp2 = new Emp(40139524,"Alok");   
       emp1.display();  
       emp2.display();  
    }  
}

Output:

Welcome to ProPoint 
Your Empid is 40139522,Your Officia name is Meena & Your Company name is ProPoint.

Welcome to ProPoint 
Your Empid is 40139524,Your Officia name is Alok & Your Company name is ProPoint.

The Java main function is static, why?

It is due to the fact that calling a static method is not needed of the object. If it were a non-static method, the problem of excessive memory allocation would arise since JVM first constructs an object before calling the main() method.




Java static block

If a calculation is required to initialise static variables, you can create a static block that will run only once, at class initialization.

  • Static block in java is used to initialize the static data member.
  • During classloading, a Java static block is executed before the main method.

How to declare Java static block
 // Java  program using static variable  
class Emp{  
    int EmpId;
    String EmpName;  
    static String CompanyName ="DockerTpoint";
    Emp(int EmpId, String EmpName ){  
        this.EmpId = EmpId;  
        this.EmpName = EmpName;  
    }
    static {
       System.out.println("Hello, Java static block initialized.");
       CompanyName = "ProPoint";  // Company Name Changed
    }
    void display(){
     System.out.println("Welcome to "+CompanyName);
     System.out.println("Your Empid is "+EmpId+",Your official name is "+EmpName+
     " & Your Company name is "+CompanyName+".\n");
    }  
}  
public class Company{  
   public static void main(String args[]){
       Emp emp1 = new Emp(40139522,"Meena");  
       Emp emp2 = new Emp(40139524,"Alok");   
       emp1.display();  
       emp2.display();  
   }  
}

Output:

Welcome to ProPoint 
Your Empid is 40139522,Your Officia name is Meena & Your Company name is ProPoint.

Welcome to ProPoint 
Your Empid is 40139524,Your Officia name is Alok & Your Company name is ProPoint.



Java Static Classes

A class can only be made static if it is nested. We cannot use the static modifier on top-level classes, but we can use it on nested classes. Such classes are known as Nested static classes. A reference to the Outer class is not required for a nested static class. In this scenario, a static class cannot access non-static Outer class members.


How to declare Java Static Classes
 // Java  program using Static Classes  
class Emp{  
	private static String CompanyName = "DockerTpoint";	
	 static class NestedClass {
		 void display(){
			  System.out.println("Welcome to "+CompanyName);
		  }
           }
}  
public class Company{  
	public static void main(String args[]){
		Emp.NestedClass object=new Emp.NestedClass();
		object.display();
	}  
}



Output:

Welcome to DockerTpoint 


Java this Keyword Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.